package com.metadata_system.system.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.metadata_system.common.db.entity.Message;
import com.metadata_system.common.enums.AllConst;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MessageResponse {
    private Integer id;
    private Integer projectId;
    private String projectName;
    private String content;
    private Object isRead;
    private Object type;
    private String params;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdTime;

    public static List<MessageResponse> fromEntity(final List<Message> messages) {
        return messages.stream().map(MessageResponse::fromEntity).collect(Collectors.toList());
    }

    public static MessageResponse fromEntity(Message message){
        return MessageResponse.builder()
                .id(message.getId())
                .projectId(message.getProjectId())
                .projectName(message.getProjectName())
                .content(message.getContent())
                .isRead(AllConst.IdName(message.getIsRead(),AllConst.ReadStatusList))
                .type(AllConst.IdName(message.getType(),AllConst.MessageTypeMap))
                .params(message.getParams())
                .createdTime(message.getCreatedTime())
                .build();
    }
}